<?php


namespace App\Service;

class Page
{
    public $listRows; // 列表每页显示行数
    public $totalRows; // 总行数
    public $rollPage = 11;// 分页栏每页显示的页数

    private $nowPage = 1;

    // 分页显示定制
    private $config = array(
        'prev' => '<<',
        'next' => '>>',
    );

    /**
     * 架构函数
     * @param array $nowPage 总的记录数
     * @param array $totalRows 总的记录数
     * @param array $listRows 每页显示记录数
     */
    public function __construct($totalRows, $nowPage=1, $listRows = 20)
    {
        $this->totalRows = $totalRows; //设置总记录数
        $this->listRows  = $listRows;  //设置每页显示行数
        $this->nowPage   = $nowPage > 0 ? $nowPage : 1;;
    }

    /**
     * 定制分页链接设置
     * @param string $name 设置名称
     * @param string $value 设置值
     */
    public function setConfig($name, $value)
    {
        if(isset($this->config[$name])){
            $this->config[$name] = $value;
        }
    }

    // 组装分页
    public function show()
    {
        $totalRows = intval($this->totalRows);
        $listRows  = intval($this->listRows);
        if($totalRows == 0){
            return [];
        }


        //  计算分页信息
        $totalPages = ceil($totalRows / $listRows); //总页数
        if(!empty($totalPages) && $this->nowPage > $totalPages){
            $this->nowPage = $totalPages;
        }

        /* 计算分页临时变量 */
        $now_cool_page      = $this->rollPage / 2;
        $now_cool_page_ceil = ceil($now_cool_page);


        //上一页 (如果没有上一页就不现实)
        $prev   = [];
        $upPage = $this->nowPage - 1;
        if($upPage > 0){
            $prev = ['page' => $upPage, 'title' => $this->config['prev'],];
        }


        //下一页
        $next     = [];
        $downPage = $this->nowPage + 1;
        if($downPage <= $totalPages){
            $next = ['page' => $downPage, 'title' => $this->config['next'],];
        }

        //第一页
        $first = [];
        if($totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
            $first = ['page' => 1, 'title' => '...1',];
        }

        //最后一页
        $end = [];
        if($totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $totalPages){
            $end = ['page' => $totalPages, 'title' => '...'.$totalPages,];
        }

        //数字连接
        $links = [];
        for($i = 1; $i <= $this->rollPage; $i++){
            // 获取页数
            if(($this->nowPage - $now_cool_page) <= 0){
                $page = $i;
            } elseif(($this->nowPage + $now_cool_page - 1) >= $totalPages){
                $page = $totalPages - $this->rollPage + $i;
            } else{
                $page = $this->nowPage - $now_cool_page_ceil + $i;
            }
            // 页数
            if($page > 0){
                if($page != $this->nowPage){
                    if($page <= $totalPages){
                        $row = ['page' => $page, 'title' => $page,];
                    } else{
                        break;
                    }
                } else{
                    if($totalPages != 1){
                        $row = ['page' => $page, 'title' => $page, 'current' => 1];
                    }
                }
            }
            // 组合
            if(!empty($row)){
                $links[] = $row;
            }
        }

        // 组合
        $result = ['total' => $totalPages, 'prev' => $prev, 'next' => $next, 'first' => $first, 'end' => $end, 'pages' => $links,];

        return $result;
    }
}
